import { AfterViewInit, ChangeDetectorRef, Component, forwardRef, Inject, Injector, NgZone, OnInit, ViewChild } from '@angular/core';
import { SplitComponent } from 'angular-split';
import { NzMessageService } from 'ng-zorro-antd/message';
import { getRealPath } from '../../../api/util';
import { ElectronService } from '../../../service/electron.service';
import { NotifyService } from '../../../service/notify.service';
import { SessionService } from '../../../service/session.service';
import { MyTabPanel } from '../../public/tabview/tabview.component';
import { BaseTabComponent } from '../base-tab/base-tab.component';
import { DragService } from './drag.service';
import { FilelistComponent } from './filelist/filelist.component';

@Component({
  selector: 'app-sftp',
  templateUrl: './sftp.component.html',
  styleUrls: ['./sftp.component.css']
})
export class SftpComponent extends BaseTabComponent implements OnInit, AfterViewInit {
  localPath = 'C:\\';
  remotePath = '/';
  //localFiles =[]
  //remoteFiles=[]
  //isLocalSpinning = false
  //isRemoteSpinning = false
  isSpinning = false;

  //constructor(private notify: NotifyService, private sessionService: SessionService, private electon: ElectronService, private message: NzMessageService) { }
  private electron: ElectronService;
  private message: NzMessageService;
  private dragSvc: DragService;
  private cd: ChangeDetectorRef;
  constructor(@Inject(forwardRef(() => MyTabPanel)) tabPanel: MyTabPanel, protected injector: Injector, private ngZone: NgZone) {
    super(tabPanel, injector.get(SessionService), injector.get(NotifyService));
    this.electron = injector.get(ElectronService);
    this.message = injector.get(NzMessageService);
    this.dragSvc = injector.get(DragService);
    this.cd = injector.get(ChangeDetectorRef);
  }

  @ViewChild('local') localFileList: FilelistComponent;
  @ViewChild('remote') remoteFileList: FilelistComponent;
  @ViewChild('mySplit') splitEl: SplitComponent;
  resizeFileList() {
    this.localFileList.setTableSize();
    this.remoteFileList.setTableSize();
  }
  ngOnInit(): void {
    if (this.electron.isElectron)
      this.localPath = this.electron.app.getPath('home');
    else
      this.localPath = 'C:\\';
    this.dragSvc.onDragDrop$.subscribe(data => {
      if (data.listType == 'Local' && this.localFileList.session.sessionid == data.sessionId) {
        this.upload(data.fileList, data.destPath);
      } else if (data.listType == 'Remote' && this.remoteFileList.session.sessionid == data.sessionId) {
        this.download(data.fileList, data.destPath);
      }
    })
  }
  ngAfterViewInit() {
    this.splitEl.dragProgress$.subscribe(x => this.ngZone.run(() => this.resizeFileList()));
  }

  onTabActive() {
    setTimeout(() => this.resizeFileList());
  }
  onWindowResize() {
    this.resizeFileList();
  }
  progress1 = 0;
  progress2 = 0;
  transType: string;
  transFileName: string;
  showProgress = false;
  setProgress(data) {
    if (data == '100') {
      this.showProgress = false;
      return;
    }
    this.transType = data.type;
    this.transFileName = data.file;
    this.progress1 = Math.round(100 * data.fileTrans / data.fileSize);
    this.progress2 = Math.round(100 * data.totalTrans / data.totalSize);
    this.cd.detectChanges();
  }
  resetProgress() {
    this.showProgress = true;
    this.transType = 'Downloading';
    this.transFileName = '';
    this.progress1 = 0;
    this.progress2 = 0;
    this.remoteFileList.session.progress$.subscribe(data => this.setProgress(data));
  }
  upload(fileList, destPath = null) {
    if (!this.remoteFileList.session) return;
    this.resetProgress();
    let remotePath = this.remotePath;
    if (destPath) remotePath = getRealPath(remotePath, destPath, 'Remote');
    this.isSpinning = true;
    this.remoteFileList.session.upload(this.localPath, remotePath, fileList).then(() => {
      this.remoteFileList.onRefresh();
      this.isSpinning = false;
      this.setProgress('100');
    }).catch((error) => {
      this.remoteFileList.onRefresh();
      this.isSpinning = false;
      this.message.create('error', 'upload failed:' + error);
    })
  }
  download(fileList, destPath = null) {
    if (!this.remoteFileList.session) return;
    this.resetProgress();
    let localPath = this.localPath;
    if (destPath) localPath = getRealPath(localPath, destPath, 'Local');
    this.isSpinning = true;
    this.remoteFileList.session.download(localPath, this.remotePath, fileList).then(() => {
      this.localFileList.onRefresh();
      this.isSpinning = false;
      this.setProgress('100');
    }).catch((error) => {
      this.localFileList.onRefresh();
      this.isSpinning = false;
      this.message.create('error', 'download failed:' + error);
    })
  }
}
